package com.yknn.bluetoothprintlib.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.yknn.bluetoothprintlib.R;
import com.yknn.bluetoothprintlib.bean.ItemPaddingBean;
import com.yknn.bluetoothprintlib.bean.Regulation;

import java.util.ArrayList;
import java.util.List;

public class ParameterAdapter extends BaseAdapter {

    private String TAG = this.getClass().getName();
    private Context mContext;
    private List<ItemBean> itemBeans = new ArrayList<>();
    private boolean[] checkedArry ;
    private int count;
    private int start;
    private int end;
    private int right = 80;
    private int left = 80;
    private int top = 5;
    private int bottm = 5;

    public ParameterAdapter(Context mContext, List<String> parNameLits , Regulation regulation , ItemPaddingBean paddingBean) {
        this.mContext = mContext;
        this.count = count = (int)Math.ceil(parNameLits.size()/3);
        int allNum = parNameLits.size();
        int tagNum = allNum -1 ;
        for (int i = 0; i < count; i++) {
            itemBeans.add(new ItemBean(i*3 <= tagNum ?  parNameLits.get(i*3) : null,
                    (i*3+1) <= tagNum ? parNameLits.get(i*3+1) :null,
                    (i*3+2) <= tagNum ? parNameLits.get(i*3+2) :null));
        }
        this.start = regulation.getStart();
        this.end = regulation.getEnd();
        this.checkedArry = new boolean[parNameLits.size()];
        this.bottm = paddingBean.getBottom();
        this.top = paddingBean.getTop();
        this.left = paddingBean.getLeft();
        this.right = paddingBean.getRight();
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return itemBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lv_parameter, null);
            holder = new ViewHolder();
            holder.ll = convertView.findViewById(R.id.ll_line);
            holder.cb0 =  convertView.findViewById(R.id.checkbox0);
            holder.cb1 =  convertView.findViewById(R.id.checkbox1);
            holder.cb2 =  convertView.findViewById(R.id.checkbox2);
            convertView.setTag(holder);
            holder.ll.setPadding(left ,top,right,bottm);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.cb0.setText(itemBeans.get(position).getItem0());
        holder.cb1.setText(itemBeans.get(position).getItem1());
        holder.cb2.setText(itemBeans.get(position).getItem2());

        final int local0 = position*3 + 0;
        final int local1 = position*3 + 1;
        final int local2 = position*3 + 2;

//        Log.d(TAG, "getView: local0 " + local0 );
//        Log.d(TAG, "getView: local1 " + local1 );
//        Log.d(TAG, "getView: local2 " + local2 );

        holder.cb0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedArry[local0] = holder.cb0.isChecked();
            }
        });

        holder.cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedArry[local1] = holder.cb1.isChecked();
            }
        });

        holder.cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedArry[local2] = holder.cb2.isChecked();
            }
        });

        if (start<= local0 && local0 <=end){
            holder.cb0.setChecked(true);
            Log.d(TAG, "getView: " + local0 );
        }else {
            holder.cb0.setChecked(false);
        }

        if (start<= local1 && local1 <=end){
            holder.cb1.setChecked(true);
            Log.d(TAG, "getView: " + local1);
        }else {
            holder.cb1.setChecked(false);
        }

        if (start<= local2 && local2 <=end){
            holder.cb2.setChecked(true);
            Log.d(TAG, "getView: " + local2);
        }else {
            holder.cb2.setChecked(false);
        }

        return convertView;
    }


    public boolean[] getCheckedArry() {
        return checkedArry;
    }



    static class ViewHolder {
        LinearLayout ll;
        CheckBox cb0;
        CheckBox cb1;
        CheckBox cb2;
    }

    class ItemBean{

        private String item0;
        private String item1;
        private String item2;

        public ItemBean(String item0, String item1, String item2) {
            this.item0 = item0;
            this.item1 = item1;
            this.item2 = item2;
        }

        public String getItem0() {
            return item0;
        }

        public void setItem0(String item0) {
            this.item0 = item0;
        }

        public String getItem1() {
            return item1;
        }

        public void setItem1(String item1) {
            this.item1 = item1;
        }

        public String getItem2() {
            return item2;
        }

        public void setItem2(String item2) {
            this.item2 = item2;
        }
    }

}
